package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.exportRFCPanel.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.database.DatabaseConnection;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.exportRFC.ActualiseNewsListThread;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.News;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.exportRFCPanel.ExportRFCPanel;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.exportRFCPanel.exportRFCPanelSubs.ShowNewsDialog;

public class SetNewsToKnowDialodActionlistener implements ActionListener {

	private ShowNewsDialog newsDialog;
	private ExportRFCPanel exportRFCPanel;
	private String flag;

	public SetNewsToKnowDialodActionlistener(ExportRFCPanel exportRFCPanel,
			ShowNewsDialog newsDialog, String flag) {
		this.newsDialog = newsDialog;
		this.exportRFCPanel = exportRFCPanel;
		this.flag = flag;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		this.setNewsToKnown();
		this.updateNewstable();
		this.newsDialog.dispose();
	}

	private void setNewsToKnown() {
		
		DatabaseConnection connection = new DatabaseConnection();
		connection.connect();
		
		String rfcID = this.newsDialog.getNews().getRfcID();
		
		String query = "UPDATE `cms_news` SET `"+News.TC_KNOWN +"` = '"+flag+"' WHERE `"+News.TC_RFC_ID+"` = '"+rfcID+"'";
				
		connection.insertquery(query);
		
		connection.disconnect();		
	}

	private void updateNewstable() {

		ActualiseNewsListThread actualiseNewsListThread = new ActualiseNewsListThread(
				exportRFCPanel);
		actualiseNewsListThread.start();

	}
}
